package com.example.bishetianqi;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class DeleteCityAdapter extends BaseAdapter {

    Context context;
    List<String>citys;
    List<String>deletecitys;

    public DeleteCityAdapter(Context context, List<String> citys, List<String> deletecitys) {
        this.context = context;
        this.citys = citys;
        this.deletecitys = deletecitys;
    }

    @Override
    public int getCount() {
        return citys.size();
    }

    @Override
    public Object getItem(int position) {
        return citys.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.item_deletecity,null);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        final String city=citys.get(position);
        viewHolder.tv.setText(city);
        viewHolder.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                citys.remove(city);
                deletecitys.add(city);
                notifyDataSetChanged();//提示适配器更新
            }
        });
        return convertView;
    }

    class ViewHolder{
        TextView tv;
        ImageView iv;
        public ViewHolder(View itemview){
            tv=itemview.findViewById(R.id.item_delete_tv);
            iv=itemview.findViewById(R.id.item_delete_iv);
        }
    }
}
